package customer.zspt.cn.customer.components.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.Widget.ContactItemView;
import customer.zspt.cn.customer.bean.SkillListDao;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.SPUtils;

/**
 * Created by Administrator on 2018/6/18 0018.群组
 */

public class group extends Activity{
    private List<SkillListDao.SkillList> data;
    @ViewInject(R.id.group_lv)
    private ListView group_lv;
    @ViewInject(R.id.title)
    private View title;
    @ViewInject(R.id.UserName)
    private TextView UserName;
    @ViewInject(R.id.back)
    private ImageView back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group);
        ViewUtils.inject(this);
        UserName.setText("群聊列表");
        View view = findViewById(R.id.empty_layout);
        group_lv.setEmptyView(view);
        pushData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    //解析
    private void pushData() {
        String skillList = SPUtils.getString(this, constant.SKILL, "");
        System.out.println(skillList);
        try{
            if (!TextUtils.isEmpty(skillList)) {
                Gson gson = new Gson();
                SkillListDao skillListDao = gson.fromJson(skillList, SkillListDao.class);
                data = skillListDao.data;
                GroupAdapter adapter = new GroupAdapter();
                group_lv.setAdapter(adapter);
                setClickLister();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    private void setClickLister() {
        group_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent item = new Intent(getApplicationContext(), group_view.class);
                item.putExtra("skillId", data.get(i).skillId);
                startActivity(item);
            }
        });
    }


    class GroupAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ContactItemView group_item = new ContactItemView(getApplicationContext());
            group_item.setIcon(R.drawable.group_icon);
            group_item.setText(data.get(i).skillTitle);
            return group_item;
        }
    }
}
